(ns tala.util
  (:use hiccup.core)
  (:use hiccup.page))

(defn respond-html [title & content]
  (html
   (doctype :xhtml-strict)
   (xhtml-tag "en"
              [:head
               [:meta {:http-equiv "Content-type"
                       :content "text/html; charset=utf-8"}]
               [:link {:href "/tala/style.css" ;FIXME: this is wiring in the deployment location!
                       :rel "stylesheet"
                       :type "text/css"}]
               [:title (str "Tala: " title)]]
              [:body
               [:h1 title]
               content
               [:div {:class "signature"}
                [:a {:href "http://astrodabis.roe.ac.uk/"} "The AstroDAbis project"]]])))

;; PARSE-ACCEPT-HEADER : string -> sequence
;; Given a string, return a list of headers as a sequence of
;; [type subtype], where type and subtype are
;; strings or false (meaning '*').  At present, we ignore parameters.
;; The result is sorted by priority.  Returns an error on parsing failures.
;;
;; In fact, right now we do no such thing, and simply return a sequence containing the first MIME type we find.
;; See code/podcasting/src/lib/http-utilities.rkt for a full-featured version (in Racket)
;; Parse the content of the HTTP Accept header, as defined in RFC
;; 2616, section 14.1.  See http://www.ietf.org/rfc/rfc2616.txt
;;
;;       Accept         = "Accept" ":"
;;                        #( media-range [ accept-params ] )
;;
;;       media-range    = ( "*/*"
;;                        | ( type "/" "*" )
;;                        | ( type "/" subtype )
;;                        ) *( ";" parameter )
;;       accept-params  = ";" "q" "=" qvalue *( accept-extension )
;;       accept-extension = ";" token [ "=" ( token | quoted-string ) ]
;;       type           = token
;;       subtype        = token
;;       token          = 1*<any CHAR except CTLs or separators>
;;       separators     = "(" | ")" | "<" | ">" | "@"
;;                      | "," | ";" | ":" | "\" | <">
;;                      | "/" | "[" | "]" | "?" | "="
;;                      | "{" | "}" | SP | HT

(defn parse-accept-header [header]
  (let [mimetype #"^[a-zA-Z0-9!#$%&*+=_|~-]+/[a-zA-Z0-9!#$%&*+=_|~-]+"] ;not quite all legal characters, but...
    (if header
      (let [m (re-find mimetype header)]
        (seq (if m [m] [])))
      (seq []))))
